<?php

namespace App\Admin\Controllers;


use App\Notice;
use App\Topic;

class NoticeController extends Controller
{
    //专题列表页面
    public function index()
    {
        $notices = Notice::all();
        
        return view('admin/notice/index', compact('notices'));
    }

    //专题创建页面
    public function create()
    {
        return view('admin/notice/create');
    }

    //保存通知
    public function store()
    {
        $this->validate(request(), [
            'title'  => 'required|min:3',
            'content' => 'required|max:255'
        ]);

        $title = request('title');
        $content = request('content');
        $notice = Notice::create(compact('title', 'content'));

        //分发任务
        $this->dispatch(new \App\Jobs\SendMessage($notice));

        return redirect('/admin/notices');
    }

    //删除专题
    public function destroy(Topic $topic)
    {
        $topic->delete();

        return [
            'error' => 0,
            'msg'   => ''
        ];
    }
}
